package com.zb.dao.ext.station;

import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSON;
import com.zb.dao.base.BaseDao;
import com.zb.dao.base.OmsCommonDao;
import com.zb.entity.base.JsonCommon;
import com.zb.entity.base.JsonForList;
import com.zb.entity.exception.OmsDaoException;
import com.zb.entity.station.OmsStation;
import com.zb.util.OmsConstant;
import com.zb.util.StringUtil;
import com.zb.vo.oms.ChargerExportVO;
import com.zb.vo.oms.StationExportVO;

/**
 * 电站基础信息数据操作Dao
 * 
 * @author pengyu
 * 
 */
@Repository
public class OmsStationDao extends OmsCommonDao<OmsStation> {

	@SuppressWarnings("rawtypes")
	public JsonForList getStationList(OmsStation station,String oper_id) {
		List<OmsStation>  list = new ArrayList<OmsStation>();
		StringBuilder sb = new StringBuilder(" where 1 = 1 ");
		if(station!=null){
			if(StringUtil.isNotBlank(station.getStation_id())){
				sb.append(" and station_id =  '"+station.getStation_id()+"'");
			}if(StringUtil.isNotBlank(station.getOper_merchant_id())){
				if(station.getOper_merchant_id().equals("0")){//自营
					sb.append(" and oper_merchant_id =  '0001'");
				}else if(station.getOper_merchant_id().equals("1")){//第三方站点
					sb.append(" and oper_merchant_id !=  '0001'");
				}else{
					sb.append(" and oper_merchant_id =  '"+station.getOper_merchant_id()+"'");
				}
			}if(station.getValid_flag()!=null){
				sb.append(" and valid_flag =  "+station.getValid_flag());
			}if(station.getStation_type()!=null){
				sb.append(" and station_type =  "+station.getStation_type());
			}if(station.getCurrent_state()!=null){
				sb.append(" and current_state =  "+station.getCurrent_state());
			}if(station.getIs_allow_open()!=null){
				sb.append(" and is_allow_open =  "+station.getIs_allow_open());
			}if(StringUtil.isNotBlank(station.getStation_name())){
				sb.append(" and station_name like  '%"+station.getStation_name()+"%'");
			}if(StringUtil.isNotBlank(station.getDistrict_code())){
				sb.append(" and district_code like  '%"+station.getDistrict_code()+"%'");
			}
		}
		if(StringUtil.isNotBlank(oper_id)){
			sb.append(" and station_id in (select station_id from oms_oper_station where oper_id = '"+oper_id+"' )");
		}
		try {
			list = findListByCondition(new OmsStation(), sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
        JsonForList<OmsStation>  jList = new  JsonForList<OmsStation>(); 
        jList.setCode(JsonForList.SuccessCode);
        jList.setMsg("ok");
        jList.setList(list);
		return jList;
	}

	@SuppressWarnings("rawtypes")
	public JsonForList addStation(OmsStation station) {
		JsonForList json = new JsonForList();
		
		// 按规则生成id
		try {
			save(station);
			json.setCode(OmsConstant.SuccessCodeEnum.SuccessCode.value());
			json.setMsg("save success");
			return json;
		} catch (Exception e) {
			json.setCode(OmsConstant.ErrorCodeEnum.SqlExecException.value());
			json.setMsg("save Exception");
			e.printStackTrace();
			return json;
		} 
	}

	@SuppressWarnings("rawtypes")
	public JsonForList updateStation(OmsStation station) {
		JsonForList json = new JsonForList();
		try {
			updateObject(station);
			json.setCode(OmsConstant.SuccessCodeEnum.SuccessCode.value());
			json.setMsg("update success");
			return json;
		} catch (Exception e) {
			json.setCode(OmsConstant.ErrorCodeEnum.SqlExecException.value());
			json.setMsg("update Exception");
			e.printStackTrace();
			return json;
		} 
	}

	@SuppressWarnings("rawtypes")
	public JsonForList deleteStation(List<String> list) {
		JsonForList json = new JsonForList();
		JsonCommon jsonc = new JsonCommon();
		StringBuilder sb =new StringBuilder("update oms_station set valid_flag = 0 where 1 = 1 ");
		if(list.size()<=0){
			json.setCode(OmsConstant.ErrorCodeEnum.SqlExecException.value());
			json.setMsg("stationIdList null");
			return json;
		}
		sb.append(" and station_id in (");
		
		
//		for(String id :list){
//			OmsStation station = new OmsStation();
//			station.setStation_id(id);
////			station.setValid_flag(0);
//			this.LogicDeleteObject(station);
//		}	
//		json.setCode("OmsConstant.SuccessCodeEnum.SuccessCode.value()");
//		json.setMsg("loginDelStation success");
//		return json;
		
		
		for(String id :list){
			sb.append("'"+id+"',");
		}
		sb.deleteCharAt(sb.length()-1);
		sb.append(")");
		try {
			exec(sb.toString());
			json.setCode(OmsConstant.SuccessCodeEnum.SuccessCode.value());
			json.setMsg("loginDelStation success");
			return json;
		} catch (Exception e) {
			json.setCode(OmsConstant.ErrorCodeEnum.SqlExecException.value());
			json.setMsg("loginDelStation Exception");
			e.printStackTrace();
			return json;
		} 
	}

	public JsonForList getStationListByPage(OmsStation station, int pageNo,int pageSize,String sortName,int sortVal,String oper_id) {
		List<OmsStation>  list = new ArrayList<OmsStation>();
		StringBuilder sb = new StringBuilder(" where 1 = 1 ");
		if(station!=null){
			if(StringUtil.isNotBlank(station.getStation_id())){
				sb.append(" and station_id =  '"+station.getStation_id()+"'");
			}if(StringUtil.isNotBlank(station.getOper_merchant_id())){
				if(station.getOper_merchant_id().equals("0")){//自营
					sb.append(" and oper_merchant_id =  '0001'");
				}else if(station.getOper_merchant_id().equals("1")){//第三方站点
					sb.append(" and oper_merchant_id !=  '0001'");
				}else{
					sb.append(" and oper_merchant_id =  '"+station.getOper_merchant_id()+"'");
				}
			}if(station.getValid_flag()!=null){
				sb.append(" and valid_flag =  "+station.getValid_flag());
			}if(station.getStation_type()!=null){
				sb.append(" and station_type =  "+station.getStation_type());
			}if(station.getCurrent_state()!=null){
				sb.append(" and current_state =  "+station.getCurrent_state());
			}if(station.getIs_allow_open()!=null){
				sb.append(" and is_allow_open =  "+station.getIs_allow_open());
			}if(StringUtil.isNotBlank(station.getStation_name())){
				sb.append(" and station_name like  '%"+station.getStation_name()+"%'");
			}if(StringUtil.isNotBlank(station.getDistrict_code())){
				sb.append(" and district_code like  '%"+station.getDistrict_code()+"%'");
			}
		}
		if(StringUtil.isNotBlank(oper_id)){
			sb.append(" and station_id in (select station_id from oms_oper_station where oper_id = '"+oper_id+"' )");
		}
		sb.append(" limit  ");
		sb.append((pageNo--)*pageSize);
		sb.append(",");
		sb.append(pageSize);
		//排序匹配
		if(sortName!=null && !"".equals(sortName)){
			sb.append(" ORDER BY ");
			//tableName（主表）表的排序条件
			sb.append("`");
			sb.append(sortName);
			sb.append("`");
			if(sortVal==1){
				sb.append(" ASC");
			}
			else{
				sb.append(" DESC");
			}
		}
		list = findListByCondition(new OmsStation(), sb.toString());
        JsonForList<OmsStation>  jList = new  JsonForList<OmsStation>(); 
        jList.setCode(JsonForList.SuccessCode);
        jList.setMsg("ok");
        jList.setList(list);
		return jList;
	}

	public void logicDelOperStationByOperId(String station_id) {
		this.getJdbcTemplate().update("update oms_oper_station set valid_flag = 0 where station_id = ? ", new Object[]{station_id});
	}

	public List<StationExportVO> exportStations(String sql)
	{
		List<StationExportVO> list = getJdbcTemplate().query(sql, new BeanPropertyRowMapper<>(StationExportVO.class));
		return list;
	}
}
